package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.fileControler.UploadFiles;
import hibernate.model.Aygit;
import hibernate.model.Aygityazilimlari;
import hibernate.model.Sirketprofil;
import hibernate.service.EmmyService;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("AygitYazilimlariBean")
@Scope("session")
public class AygitYazilimlariBean implements BeanService{

	@Autowired
	private transient EmmyService emmyService;	
	private transient SirketprofilBean sirketProBean;

	private Aygityazilimlari selectedAygitYzl;		
	private List<Aygityazilimlari> aygitYazilimList;
	
	private boolean yzlIslem = false;
	private boolean yeniYzl = false;
	private TemsilciBean temBean;
	private UploadedFile file;
	private String upTarihBitis;
	
	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void init() {
		aygitYazilimList = (List<Aygityazilimlari>) emmyService.findAll(new Aygityazilimlari());
	}

	@Override
	public String restart() {
		deleteSelectedAygitYzl();
		deleteSelectedSirket();
		init();
		return "";
	}

	@Override
	public String deleteView() {
		emmyService.delete(selectedAygitYzl);
		return "";
	}

	@Override
	public String viewNew() {
		System.out.println("yazılım new ");
		selectedAygitYzl= new Aygityazilimlari();
		temBean = (TemsilciBean) emmyService.getBean(new TemsilciBean());
		AygitBean agtBean = (AygitBean) emmyService.getBean(new AygitBean());
		agtBean.getUreticiAygitlari(temBean.getCurrentTemsilci().getSirketprofil());
		
		selectedAygitYzl.setTemsilci(temBean.getCurrentTemsilci());
		selectedAygitYzl.setSirketprofil(temBean.getCurrentTemsilci().getSirketprofil());
		
		System.out.println("sirket : "+selectedAygitYzl.getSirketprofil().getFirmaAd());
		yzlIslem = true;
		yeniYzl = true;
		return "";
	}

	@Override
	public String viewDetail() {
		System.out.println("detay detayyy");
		/*
		List<Aygityazilimlari> valueList = (List<Aygityazilimlari>) aygitYazilimTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = aygitYazilimTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentAygityazilimlari= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentAygityazilimlari.getAygitYazid());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ return "";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		System.out.println("konum : "+selectedAygitYzl.getDiskKonumu());
		emmyService.saveOrUpdate(selectedAygitYzl);
		yeniYzl = false;
		restart();
		return "list";
	}
	
	@SuppressWarnings("unchecked")
	public void getYazilimList(){
		System.out.println("yazilimList'e geldim...");
		try{
			aygitYazilimList = (List<Aygityazilimlari>) emmyService.findAll(new Aygityazilimlari());
		}catch(Exception e){
			System.out.println("Exp: "+e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public String getUreticiYazilimlari(Sirketprofil sp) {	
		System.out.println("giriyor muyum");//girmemişşşşşşş
		aygitYazilimList = (List<Aygityazilimlari>) 
				emmyService.getQuery("findAygitYazFirmaid", "ayFirmaid", sp.getFirmaid());		
		
		//if(!yeniYzl){
		//	selectedAygitYzl =null;
		//}
		return "AygitYzlList"; // Bu dönüş değeri ileride değişebilir
	}
	
	@SuppressWarnings("unchecked")
	public void getAygitYazilimlari(Aygit agt) {	
		System.out.println("aygıtın yazılımları");
		aygitYazilimList = (List<Aygityazilimlari>) 
				emmyService.getQuery("findAygitYazAygid", "ayAygtid", agt.getAygitid());						
	}
		
	@SuppressWarnings("unchecked")
	public String getDetayliAygitYazilimlari(){		
		aygitYazilimList = (List<Aygityazilimlari>) emmyService.getQuery(createQuery());
		return "";
	}
	
	public void deleteAygitYzlList(){	//deleteYzlToContext
		aygitYazilimList =null;
	}
	
	public void deleteSelectedSirket(){ 
		sirketProBean = (SirketprofilBean)emmyService.getBean(new SirketprofilBean());
		sirketProBean.deleteSelectedSirket();
	}
	
	public void deleteSelectedAygitYzl(){		
		selectedAygitYzl =null;
	}
	
	public void yazilimIslem(boolean islem){
		FacesMessage msg = null; 
		if(islem){
			if(selectedAygitYzl !=null){
				yzlIslem = islem;
			}else{
				yzlIslem =true;
				msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Seçim yapılmadı","Yeni Kayıt yapılacak");
				FacesContext.getCurrentInstance().addMessage(null, msg);
				viewNew();
			}
		}else{
			yzlIslem = islem;
			yeniYzl = false;
		}
	}
	
	public String verifyProducer(ComponentSystemEvent event){
		try{
			System.out.println("Yazilim işlem verifyingProducer");
			sirketProBean = (SirketprofilBean) emmyService.getBean(new SirketprofilBean());
			getUreticiYazilimlari(sirketProBean.getCurrentSirketpro());
			sirketProBean.setSelectedSirket(sirketProBean.getCurrentSirketpro());
			System.out.println("Yazilim işlem verifyProducer ok");
			return "";
		}catch(Exception e){
			System.out.println("Yazilim işlem sayfası erişim hatası: "+e);
			return "index";
		}
	}
	
	 @SuppressWarnings("static-access")
	 public void handleFileUpload() {  
	    	System.out.println("upload");
	    	UploadFiles upsfware = new UploadFiles();
	    	try {
	    		if(file != null){
	    			upsfware = upsfware.saveFile(file,"yazilimlar");
	    			if(upsfware.isIslem()){
	    				FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesful", file.getFileName() + " is uploaded.");  
	    				FacesContext.getCurrentInstance().addMessage(null, msg);
	    				selectedAygitYzl.setDiskKonumu("../yazilimlar/"+upsfware.getFilePath());
	    				SaveOrUpdate();
	    			}
	    			else{
	    				FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccesful", file.getFileName() + " isn't upload.");  
	    				FacesContext.getCurrentInstance().addMessage(null, msg);
					}
				}else{
					FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesful","Completed.");  
    				FacesContext.getCurrentInstance().addMessage(null, msg);
    				SaveOrUpdate();
				}
			}catch(MyException mye){
				FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccesful", file.getFileName() +" : "+ mye.getMessage());  
				FacesContext.getCurrentInstance().addMessage(null, msg);
			}
	    	catch (Exception e) {
				System.out.println("handle Error");
				e.printStackTrace();
			}
	 } 
	 
	@Override
	public String createQuery() {
		Aygityazilimlari agyYzl = selectedAygitYzl;
		String query = "from Aygityazilimlari";	
		
		if(agyYzl == null){
			return query;
		}
		if(agyYzl.getAygit().getAygitid() != 0){
			if(query.indexOf("where") != -1){
				query += " and aygitid="+agyYzl.getAygit().getAygitid()+"";
				
			}
			else{
				query += " where aygitid="+agyYzl.getAygit().getAygitid()+"";			
			}
		}
		if(agyYzl.getSirketprofil().getFirmaid() != 0){
			if(query.indexOf("where") != -1){
				query += " and firmaid="+agyYzl.getSirketprofil().getFirmaid()+"";
				
			}
			else{
				query += " where firmaid="+agyYzl.getSirketprofil().getFirmaid()+"";			
			}
		}
		if(agyYzl.getTemsilci().getKullaniciid() != 0){
			if(query.indexOf("where") != -1){
				query += " and kullaniciid="+agyYzl.getTemsilci().getKullaniciid()+"";
				
			}
			else{
				query += " where kullaniciid="+agyYzl.getTemsilci().getKullaniciid()+"";			
			}
		}
		if(agyYzl.getUploadTarihi() != null){
			if(query.indexOf("where") != -1){
				query += " and (uploadTarihi between "+agyYzl.getUploadTarihi()+" and "+upTarihBitis+")";
				
			}
			else{
				query += " where (uploadTarihi between "+agyYzl.getUploadTarihi()+" and "+upTarihBitis+")";			
			}
		}
		if(agyYzl.getVersiyonNo() != null){
			if(query.indexOf("where") != -1){
				query += " and versiyonNo="+agyYzl.getVersiyonNo()+"";
				
			}
			else{
				query += " where versiyonNo="+agyYzl.getVersiyonNo()+"";			
			}
		}
		if(agyYzl.getAciklama() != null){
			if(query.indexOf("where") != -1){
				query += " and aciklama like '%"+agyYzl.getAciklama()+"%'";
				
			}
			else{
				query += " where aciklama like '%"+agyYzl.getAciklama()+"%'";			
			}
		}
		return query;
	}

	@Override
	public void getSession() {
	}

	public Aygityazilimlari getSelectedAygitYzl() {
		return selectedAygitYzl;
	}

	public void setSelectedAygitYzl(Aygityazilimlari selectedAygitYzl) {
		this.selectedAygitYzl = selectedAygitYzl;
	}

	public List<Aygityazilimlari> getAygitYazilimList() {
		return aygitYazilimList;
	}

	public void setAygitYazilimList(List<Aygityazilimlari> aygitYazilimList) {
		this.aygitYazilimList = aygitYazilimList;
	}

	public String getUpTarihBitis() {
		return upTarihBitis;
	}

	public void setUpTarihBitis(String upTarihBitis) {
		this.upTarihBitis = upTarihBitis;
	}

	public SirketprofilBean getSirketProBean() {
		return sirketProBean;
	}

	public void setSirketProBean(SirketprofilBean sirketProBean) {
		this.sirketProBean = sirketProBean;
	}

	public boolean isYzlIslem() {
		return yzlIslem;
	}

	public void setYzlIslem(boolean yzlIslem) {
		this.yzlIslem = yzlIslem;
	}

	public TemsilciBean getTemBean() {
		return temBean;
	}

	public void setTemBean(TemsilciBean temBean) {
		this.temBean = temBean;
	}

	public UploadedFile getFile() {
		return file;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

	public boolean isYeniYzl() {
		return yeniYzl;
	}

	public void setYeniYzl(boolean yeniYzl) {
		this.yeniYzl = yeniYzl;
	}
}